package com.bawei.module_publish.ui

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_publish.R
import com.bawei.module_publish.databinding.ActivityDraftBinding
import com.qiniu.pili.droid.shortvideo.PLDraft
import com.qiniu.pili.droid.shortvideo.PLDraftBox
import com.qiniu.pili.droid.shortvideo.PLMediaFile
import java.io.File

/**
 *@Author：lxy
 *@Description:草稿箱
 *@Time：2025/7/1
 */
@Route(path = "/publish/DraftActivity")
class DraftActivity : BaseActivity() {

    private val binding: ActivityDraftBinding by lazy {
        ActivityDraftBinding.inflate(layoutInflater)
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    private lateinit var mDraftBox: PLDraftBox
    private lateinit var mDrafts: List<PLDraft>
    private lateinit var mAdapter: DraftAdapter

    override fun initView() {
        mDraftBox = PLDraftBox.getInstance(this)
        mDrafts = mDraftBox.allDrafts
        mAdapter = DraftAdapter(this)
        binding.draftList.adapter = mAdapter
    }

    override fun initEvent() {
        binding.draftList.onItemClickListener =
            AdapterView.OnItemClickListener { _: AdapterView<*>?, view: View?, i: Int, l: Long ->
                val draft = mDrafts[i]
                ARouter.getInstance().build("/publish/VideoRecordActivity")
                    .withString(VideoRecordActivity.DRAFT,draft.tag).navigation()
            }

        binding.draftList.onItemLongClickListener =
            AdapterView.OnItemLongClickListener { adapterView: AdapterView<*>?, view: View?, pos: Int, l: Long ->
                val alertDialog = AlertDialog.Builder(this)
                    .setTitle(R.string.dlg_delete_draft)
                    .setPositiveButton(getString(R.string.dlg_yes))
                    { dialogInterface, i ->
                        val draft = mDrafts[pos]
                        PLDraftBox.getInstance(this)
                            .removeDraftByTag(draft.tag, true)
                        updateView()
                    }
                alertDialog.show()
                true
            }
    }

    override fun onResume() {
        super.onResume()
        updateView()
    }

    private fun updateView() {
        if (mAdapter != null) {
            mDrafts = PLDraftBox.getInstance(this).allDrafts
            if (mDrafts.size == 0) {
                binding.draftList.visibility = View.GONE
                binding.tvNullView.visibility = View.VISIBLE
            } else {
                binding.draftList.visibility = View.VISIBLE
                binding.tvNullView.visibility = View.GONE
            }
            mAdapter.notifyDataSetChanged()
        }
    }

    inner class DraftAdapter(var mContext: Context) : BaseAdapter() {
        override fun getCount(): Int {
            return mDrafts.size
        }

        override fun getItem(pos: Int): Any {
            return mDrafts.get(pos)
        }

        override fun getItemId(pos: Int): Long {
            return 0
        }

        override fun getView(pos: Int, convertView: View, parent: ViewGroup): View {
            var convertView = convertView
            val textView: TextView
            val imageView: ImageView
            if (convertView == null) {
                convertView =
                    LayoutInflater.from(mContext).inflate(R.layout.item_draft, parent, false)
                textView = convertView.findViewById<TextView>(R.id.draft_tag)
                textView.setText(mDrafts.get(pos).getTag())
                imageView = convertView.findViewById<ImageView>(R.id.draft_thumbnail)
                val file: File = File(mDrafts.get(pos).getSectionFilePath(0))
                if (file.exists()) {
                    val mediaFile = PLMediaFile(file.absolutePath)
                    val frame = mediaFile.getVideoFrameByTime(100, false)
                    mediaFile.release()
                    if (frame != null) {
                        imageView.setImageBitmap(frame.toBitmap())
                    } else {
                        imageView.setBackgroundResource(R.drawable.bg_invalid_draft)
                    }
                }
            }
            return convertView
        }
    }
}